package com.CmJava.utils;

import com.CmJava.entity.ResultEntity;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import java.io.*;

public class BackupUtil {

    //备份某篇博客
    public static ResultEntity backupTheBlog(String key, Object data) {
        File target = new File(File.separator + "opt" +
                File.separator + "MyProject" +
                File.separator + "MyWeb" +
                File.separator + "backup" +
                File.separator + "blog" +
                File.separator + key + ".txt"
        );

        String successData = "备份博客(" + key + ")成功，备份路径为" + target.getAbsolutePath();
        String FailureDataByIOException = "备份博客(" + key + ")失败，原因是IO异常";
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(target)));
            bufferedWriter.write((String) data);
            bufferedWriter.flush();
            return ResultEntity.successWithMessage(successData);
        } catch (IOException e) {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }

                if (target.exists()) {
                    target.delete();
                }

                return ResultEntity.failureWithMessage(FailureDataByIOException);
            }
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }
        return ResultEntity.failureWithMessage(FailureDataByIOException);
    }

    //下载压缩包
    public static ResultEntity downBackupBlogs() {
        File target = new File(File.separator + "opt" +
                File.separator + "MyProject" +
                File.separator + "MyWeb" +
                File.separator + "backup" +
                File.separator + "blog"
        );

        File save = new File(File.separator + "opt" +
                File.separator + "MyProject" +
                File.separator + "MyWeb" +
                File.separator + "backup" +
                File.separator + "backupBlogs.zip"
        );

        return createZip(target, save);
    }

    //创建压缩包
    public static ResultEntity createZip(File target, File save) {
        ZipOutputStream zipOutputStream = null;
        BufferedInputStream bufferedInputStream = null;
        try {


            zipOutputStream = new ZipOutputStream(save);

            for (File backup : target.listFiles()) {

                ZipEntry zipEntry = new ZipEntry(backup.getName());
                zipOutputStream.putNextEntry(zipEntry);

                bufferedInputStream = new BufferedInputStream(new FileInputStream(backup));
                byte[] buffer = new byte[1024 * 1024];
                int read = 0;
                while ((read = bufferedInputStream.read(buffer)) != -1) {
                    zipOutputStream.write(buffer, 0, read);
                }

                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }
            }
            zipOutputStream.closeEntry();
            zipOutputStream.close();

            return ResultEntity.successWithMessage(save);
        } catch (Exception e) {
            return ResultEntity.failureWithoutMessage();
        } finally {
            try {
                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }

                if (zipOutputStream != null) {
                    zipOutputStream.close();
                }
            } catch (Exception e) {
            }
        }
    }

    public static ResultEntity backupTheProject(String key, Object data) {
        File target = new File(File.separator + "opt" +
                File.separator + "MyProject" +
                File.separator + "MyWeb" +
                File.separator + "backup" +
                File.separator + "project" +
                File.separator + key + ".txt"
        );

        String successData = "备份项目(" + key + ")成功，备份路径为" + target.getAbsolutePath();
        String FailureDataByIOException = "备份项目(" + key + ")失败，原因是IO异常";
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(target)));
            bufferedWriter.write((String) data);
            bufferedWriter.flush();
            return ResultEntity.successWithMessage(successData);
        } catch (IOException e) {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }

                if (target.exists()) {
                    target.delete();
                }

                return ResultEntity.failureWithMessage(FailureDataByIOException);
            }
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }
        return ResultEntity.failureWithMessage(FailureDataByIOException);
    }

    public static ResultEntity<Object> downBackupProject() {
        File target = new File(File.separator + "opt" +
                File.separator + "MyProject" +
                File.separator + "MyWeb" +
                File.separator + "backup" +
                File.separator + "project"
        );

        File save = new File(File.separator + "opt" +
                File.separator + "MyProject" +
                File.separator + "MyWeb" +
                File.separator + "backup" +
                File.separator + "backupProjects.zip"
        );

        return createZip(target, save);
    }
}
